//
// Created by ls on 24-1-1.
//

#include <iostream>
#include <map>
#include <random>
#include <string>
#include <utility>
#include <vector>

/*
- 公司今天招聘了10个员工（ABCDEFGHIJ），10名员工进入公司之后，需要指派员工在那个部门工作
- 员工信息有: 姓名  工资组成；部门分为：策划、美术、研发
- 随机给10名员工分配部门和工资
- 通过multimap进行信息的插入  key(部门编号) value(员工)
- 分部门显示员工信息
*/

#define CEHUA  0
#define MEISHU 1
#define YANFA  2

namespace Example
{
    int getRandomNumber(const int min, const int max)
    {
        static std::random_device rd;
        static std::mt19937 gen(rd());
        std::uniform_int_distribution<> dis(min, max);

        return dis(gen);
    }

    // 1.创建员工
    class Worker {
    public:
        Worker(std::string name, const int salary) : m_Name(std::move(name)), m_Salary(salary) {}
        ~Worker() = default;

        // 提供公共的访问方法
        [[nodiscard]] std::string getName() const {return m_Name;}
        [[nodiscard]] int getSalary() const {return m_Salary;}

    private:
        std::string m_Name;
        int m_Salary{};
    };

    void createWorker(std::vector<Worker> &v)
    {
        const std::string nameSeed = "ABCDEFGHIJ";
        for (int i = 0; i < 10; ++i) {
            v.emplace_back(
                "员工" + std::string(1, nameSeed[i]),
                getRandomNumber(10000, 19999)
                );
        }
    }

    // 员工分组
    void setGroup(const std::vector<Worker> &v, std::multimap<int, Worker> &m)
    {
        for (auto &worker : v) {
            int deptId = getRandomNumber(0, 2);
            m.emplace(deptId, worker);

            // 添加打印语句来检查部门分配
            std::cout << "员工：" << worker.getName() << " 被分配到部门：" << deptId << std::endl;
        }
    }

    void showWokerByGroup(std::multimap<int, Worker> &m, const int deptId, const std::string &deptName)
    {
        const auto [begin, end] = m.equal_range(deptId);

        std::cout << "---------------------------\n" << deptName << "部门：" << std::endl;
        for (auto it = begin; it != end; ++it) {
            std::cout << "姓名：" << it->second.getName() << "工资：" << it->second.getSalary() << std::endl;
        }
    }

    void printVector(const std::vector<Worker> &v)
    {
        for (auto &it : v) {
            std::cout << "姓名：" << it.getName() << "，薪资：" << it.getSalary() << std::endl;
        }
    }

}


int main()
{
    // 1.创建员工
    std::vector<Example::Worker> vWorker;
    createWorker(vWorker);
    Example::printVector(vWorker);

    // 2.员工分组
    std::multimap<int, Example::Worker> mWorker;
    Example::setGroup(vWorker, mWorker);

    // 3.分组显示员工
    Example::showWokerByGroup(mWorker, CEHUA, "策划");
    Example::showWokerByGroup(mWorker, MEISHU, "美术");
    Example::showWokerByGroup(mWorker, YANFA, "研发");
}